#pragma once
#ifndef __NOTIFY_H__
#define __NOTIFY_H__

#include <any>
#include <msg/message.h>
#include <arpa/inet.h>


enum class notify_event: uint32_t{
    NEW_CLIENT_CONNECT,
    TRUE_CLIENT_DISCONNECT,
    VIRTUAL_CLIENT_DISCONNECT
};

struct alignas(32) notify_head{
    notify_event event;

    notify_head(notify_event event):
        event(event) 
    { }

    void transform_to_network_byte_order(){
        htonl(static_cast<uint32_t>(event));
    }

    void transform_to_host_byte_order(){
        ntohl(static_cast<uint32_t>(event));
    }
};


// notify 不应该算是message
// notify只是本机通信
struct notify : message{
    notify(){
        set_message_type(MessageType::RESPONSE);
    }

    notify_head get_head(){
        notify_head head = *get_head_ptr();
        head.transform_to_host_byte_order();
        return head;
    }

    std::any get_body(){

    }

    size_t get_body_size(){
        return get_router_info().data_size - sizeof(notify_head);
    }

    // notify 主机内通信，应该不需要转换字节序
    static notify make_notify(notify_event event, std::span<char const> body = {}){
        notify notify;
        notify.get_head_ptr()->event = event;
        notify.get_head_ptr()->transform_to_network_byte_order();

        // notify.set_sender(req.get_router_info().to);
        // notify.set_receiver(req.get_router_info().from);
        notify.set_data_size(sizeof(notify_head) + body.size());

        // if (body.empty() == false)
        //     notify.set_data(notify.get_body_offset(), body);

        return notify;
    }

protected:
    size_t get_head_offset(){
        return ROUTER_INFO_SIZE;
    }

    notify_head* get_head_ptr(){
        return reinterpret_cast<notify_head*>(get_data_ptr(get_head_offset()));
    }

    size_t get_body_offset(){
        return get_head_offset() + sizeof(notify_head);
    }
    void* get_body_ptr(){
        return get_data_ptr(get_body_offset());
    }

private:
    static const size_t ROUTER_INFO_SIZE = sizeof(router_info);
    
    // static const int HEAD_SIZE = sizeof(request_head);
};

#endif // __NOTIFY_H__